var ssoUI = { init: function init() { var params = []; this.errorFlg = false; this.site_url = 'https://accounts.indianexpress.com'; this.api_host = this.site_url + '/api/v2'; this.headerPayload = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "X-API-KEY": "ydDxstlJCp7a0CwCKqkwtpeptJidV6fkCnaxj51xYN7jAsgdoDtZgwVsML3Zfs9ktc9j7nlgfMEdyP3m3llagEsRAxF6rtTYeCSZ7jpg+x/gTX3htkBYttdr38Y5W2zdMN0eHyt+Qje4AWI6xog1gsJelA90VNtnm3XH3JN1LGbP3BbPUdSShWhoTYvKr9xiktc1s9r98LNI9CSBFtnA/lUZi1UzC3oQvPUmqwFzyvyb25tRKse6GVBZifWibZ1LBAgqDws9YJGG9gfAbsQKvdGeI3Fw7FJAlYkaFknq5Lmgyi9ej05/X8nMzS+L3mf1OTPuMXNOmab2W+umCrJDZg==" }; if (typeof IESSO === "undefined" || IESSO === null || IESSO == '') { console.error('SSO sdk initialisation failed.'); this.errorFlg = true; } else { params = IESSO.pop(); this.qparams = ssoUI.QParams(); } this.ssoDivObj = document.getElementsByClassName('iesso-login'); if (typeof this.ssoDivObj === "undefined" || this.ssoDivObj === null || this.ssoDivObj == '') { console.error('SSO div tag not found.'); this.errorFlg = true; } this.subBanDiv = document.getElementsByClassName('epaper-banner'); this.subcribedbanner = true; if (typeof this.subBanDiv === "undefined" || this.subBanDiv === null || this.subBanDiv.length < 1) { this.subcribedbanner = false; } this.client_id = ssoUI.ValidParams(params.client_id); this.client_secret = ssoUI.ValidParams(params.client_secret); this.response_type = ssoUI.ValidParams('code'); this.redirect_uri = ssoUI.ValidParams(params.redirect_uri); this.request_uri = encodeURIComponent( ssoUI.ValidParams(params.request_uri) ); this.state = ssoUI.randomState().toString(); //auto renewal if (false !== this.qparams.hasOwnProperty('renewal') && '1' === this.qparams.renewal ) { this.subrenewal = true; } else { this.subrenewal = false; } this.auto_renwal = ssoUI.qparams //debugger if (false !== this.qparams.hasOwnProperty('ack') && this.qparams.ack.length > 50) { this.access_token = this.qparams.ack; } else { this.access_token = ssoUI.CookieRead('access_token'); } this.fingerprint_uid = ssoUI.getFingerprint(); if (this.fingerprint_uid !== '' && typeof this.fingerprint_uid !== "undefined" && this.fingerprint_uid !== null && this.errorFlg === false && this.ssoDivObj.length > 0) { ssoUI.ajaxRequest('GET', this.api_host + '/sso/user/?user_fingerprint=' + this.fingerprint_uid, this.headerPayload, '', 'fingerprint'); } else if (this.access_token !== '' && typeof this.access_token !== "undefined" && this.access_token !== null && this.errorFlg === false && this.ssoDivObj.length > 0) { this.headerPayload.Authorization = "Bearer " + this.access_token; ssoUI.ajaxRequest('GET', this.api_host + '/sso/user/', this.headerPayload, '', ''); ssoUI.ProfileUI(); ssoUI.IeLoginBtn(); } else { ssoUI.LoginUI(); // show login button ssoUI.IeLoginBtn(); if (this.subcribedbanner) { document.getElementById('subscribed_banner').style.display = 'none'; document.getElementById('non_subscribed_banner').style.display = 'block'; } } }, ValidParams: function ValidParams(v) { return typeof v !== "undefined" && v !== null && v !== '' ? v.toString() : ''; }, QParams: function QParams() { var qp, arr; if (/code|token|error/.test(window.location.hash)) { qp = location.hash.substring(1); } else { qp = location.search.substring(1); } arr = qp.split("&"); arr.forEach(function (v, i, _arr) { if (v.indexOf('=') != -1) { _arr[i] = '"' + v.replace('=', '":"') + '"'; } else { _arr[i] = ''; } }); qp = qp ? JSON.parse('{' + arr.join() + '}', function (key, value) { return key === "" ? value : decodeURIComponent(value); }) : {}; return qp; }, qstring: function qstring() { var query = "?client_id=" + this.client_id + "&response_type=" + this.response_type + "&redirect_uri=" + this.redirect_uri + "&request_uri=" + this.request_uri + "&state=" + this.state; return query; }, ProfileUI: function ProfileUI() { if ( this.subrenewal ) { var q = ssoUI.qstring(); location.href = this.site_url + "/account/order/" + q; } var txtHTML = '
'; for (var x = 0; x < this.ssoDivObj.length; x++) { this.ssoDivObj[x].innerHTML = ''; this.ssoDivObj[x].innerHTML = txtHTML; } return true; }, ProfileEV: function ProfileEV() { localStorage.removeItem('user_hit'); window.open(this.site_url + '/account/', "_blank"); }, LoginUI: function LoginUI() { var q = ssoUI.qstring(); if ( this.subrenewal ) { location.href = this.site_url + "/oauth2/authorize/" + q; } var txtHTML = '
'; for (var x = 0; x < this.ssoDivObj.length; x++) { this.ssoDivObj[x].innerHTML = ''; this.ssoDivObj[x].innerHTML = txtHTML; } }, LoginEV: function LoginEV(e) { if ('undefined' !== typeof e) { if ('m-links-btm__link-block' === e.className) { this.subscriptionGaEvent('Subscription Activity', 'Login', '', ''); } } if ('undefined' !== typeof clevertap) { clevertap.event.push("EXU_Login_Initiated"); } var q = ssoUI.qstring(); this.access_token = ssoUI.CookieRead('access_token'); if (this.access_token !== '' && typeof this.access_token !== "undefined" && this.access_token !== null && this.errorFlg === false) { location.href = window.location.href; } else { location.href = this.site_url + "/oauth2/authorize/" + q; } }, CookieWrite: function CookieWrite(name, value, days, domain, path) { var date = new Date(); days = days || 730; // two years path = path || '/'; date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); var expires = '; expires=' + date.toGMTString(); var cookieValue = name + '=' + value + expires + '; path=' + path; if (domain) { cookieValue += '; domain=' + domain; } document.cookie = cookieValue; }, CookieRead: function CookieRead(name) { var allCookie = '' + document.cookie; var index = allCookie.indexOf(name); if (name === undefined || name === '' || index === -1) return ''; var ind1 = allCookie.indexOf(';', index); if (ind1 == -1) ind1 = allCookie.length; var checkCookieValue = unescape(allCookie.substring(index + name.length + 1, ind1)); if ('access_token' === name && checkCookieValue.length < 50) return ''; return checkCookieValue; }, CookieRemove: function CookieRemove(name) { if ('' !== name && this.CookieRead(name)) { var del_domain_name = ( 'exp_token_jwt' === name ) ? ssoUI.getParentDomainName() : window.location.hostname; document.cookie = name + '=;path=/;max-age=-1; domain=' + del_domain_name; //this.CookieWrite(name, '', -1, '', '/'); } }, ajaxRequest: function ajaxRequest(method, url, headers, params, act_type) { var request = new XMLHttpRequest(); request.open(method, url, true); for ( var key in headers ) { if ( headers.hasOwnProperty( key ) ) { request.setRequestHeader( key, headers[key] ); } } request.onload = function () { if (request.status >= 200 && request.status < 400) { var obj = JSON.parse(this.responseText); var parsed_info = ''; var not_valid = []; var parent_domain = ssoUI.getParentDomainName(); if (obj.hasOwnProperty("token") && '' !== obj.token) { // user login with jwt token! if ('' != act_type) { switch (act_type) { case 'fingerprint': ssoUI.CookieWrite('access_token', obj.token, 30, window.location.hostname, '/'); ssoUI.CookieWrite('oauth_user', JSON.stringify(obj.data.user), 1, window.location.hostname, '/'); ssoUI.ProfileUI(); ssoUI.IeLoginBtn(); break; case 'redirect': ssoUI.CookieWrite('access_token', obj.token, 30, window.location.hostname, '/'); ssoUI.CookieWrite('exp_token_jwt', obj.token, 1, parent_domain, '/'); ssoUI.CookieWrite('oauth_user', JSON.stringify(obj.data.user), 1, window.location.hostname, '/'); var check_request = ssoUI.qparams.request_uri; if( check_request.search('express-plus') > 0 ) { parsed_info = obj.data.user; not_valid = ['created', 'authenticated', 'pending', 'halted', 'active']; if (typeof parsed_info.plan_status != 'undefined' && not_valid.indexOf(parsed_info.plan_status) > -1) { location.href = '/express-plus/today-newsstand/'; } else { if ('1' !== ssoUI.CookieRead('subscription_popup')) { location.href = '/express-plus/subscription-plan/#goofy'; } else { location.href = '/express-plus/subscription-plan/'; } } } else { location.href = check_request; // redirect to request uri } return false; default: ssoUI.CookieWrite('oauth_user', JSON.stringify(obj.data.user), 1, window.location.hostname, '/'); ssoUI.CookieWrite('exp_token_jwt', obj.token, 1, parent_domain, '/'); ssoUI.ProfileUI(); if (ssoUI.subcribedbanner) { parsed_info = obj.data.user; not_valid = ['created', 'authenticated', 'pending', 'halted', 'active']; if (typeof parsed_info.plan_status != 'undefined' && not_valid.indexOf(parsed_info.plan_status) > -1) { document.getElementById('subscribed_banner').style.display = 'block'; document.getElementById('non_subscribed_banner').style.display = 'none'; } else { document.getElementById('subscribed_banner').style.display = 'none'; document.getElementById('non_subscribed_banner').style.display = 'block'; } } } } else { if (false !== ssoUI.qparams.hasOwnProperty('ack') && ssoUI.qparams.ack.length > 50) { ssoUI.CookieWrite('access_token', obj.token, 30, window.location.hostname, '/'); ssoUI.CookieWrite('exp_token_jwt', obj.token, 1, parent_domain, '/'); } ssoUI.CookieWrite('oauth_user', JSON.stringify(obj.data.user), 1, window.location.hostname, '/'); if (ssoUI.subcribedbanner) { parsed_info = obj.data.user; not_valid = ['created', 'authenticated', 'pending', 'halted', 'active']; if (typeof parsed_info.plan_status != 'undefined' && not_valid.indexOf(parsed_info.plan_status) > -1) { document.getElementById('subscribed_banner').style.display = 'block'; document.getElementById('non_subscribed_banner').style.display = 'none'; } else { document.getElementById('subscribed_banner').style.display = 'none'; document.getElementById('non_subscribed_banner').style.display = 'block'; } } ssoUI.ProfileUI(); } } else { // delete jwt token cookie if ('' != act_type) { switch (act_type) { case 'redirect': location.href = ssoUI.qparams.request_uri; // redirect to request uri return false; default: ssoUI.CookieRemove('access_token'); ssoUI.CookieRemove('oauth_user'); ssoUI.CookieRemove('exp_token_jwt'); ssoUI.LoginUI(); } } else { ssoUI.CookieRemove('access_token'); ssoUI.CookieRemove('oauth_user'); ssoUI.CookieRemove('exp_token_jwt'); ssoUI.LoginUI(); } } } else { // We reached our target server, but it returned an error ssoUI.CookieRemove('access_token'); ssoUI.CookieRemove('oauth_user'); ssoUI.CookieRemove('exp_token_jwt'); ssoUI.LoginUI(); ssoUI.IeLoginBtn(); } }; request.onerror = function () { // There was a connection error of some sort ssoUI.CookieRemove('access_token'); ssoUI.CookieRemove('oauth_user'); ssoUI.CookieRemove('exp_token_jwt'); ssoUI.LoginUI(); }; request.send(params); }, GetAjax: function GetAjax() { //Do nothing at this time. ssoUI.CookieRemove('access_token'); ssoUI.CookieRemove('oauth_user'); ssoUI.CookieRemove('exp_token_jwt'); ssoUI.LoginUI(); }, randomState: function randomState() { var sizestate = 6; var State = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < sizestate; i++ ) { State += characters.charAt(Math.floor(Math.random() * charactersLength)); } if (ssoUI.CookieRead('ssostate')) { State = ssoUI.CookieRead('ssostate'); } else { ssoUI.CookieWrite('ssostate', State, 1, window.location.hostname, '/'); } return State; }, oAuth2Redirect: function oAuth2Redirect() { var isValid, sentState; var client_id = this.client_id; var client_secret = this.client_secret; var redirect_uri = ssoUI.redirect_uri; var code = ssoUI.qparams.code; var state = ssoUI.qparams.state; var request_uri = ssoUI.qparams.request_uri; var params = 'code=' + code + '&client_id=' + client_id + '&client_secret=' + client_secret + '&grant_type=authorization_code&scope=userinfo&redirect_uri=' + redirect_uri; sentState = ssoUI.CookieRead('ssostate'); console.log('cookie deleted: ssostate' + ssoUI.CookieRemove('ssostate')); isValid = state === sentState; if (!isValid) { console.log("Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"); if (request_uri.search('subscription-plan') > -1) { window.location.href = '/express-plus/today-newsstand/'; // redirect to request uri } else { window.location.href = request_uri; // redirect to request uri } return false; } if (typeof code !== "undefined" && code !== null && code != '' && typeof request_uri !== "undefined" && request_uri !== null && request_uri != '') { ssoUI.ajaxRequest('POST', this.site_url + '/oauth2/authorize/token', this.headerPayload, params, 'redirect'); } else { alert("[Authorization failed]: no accessCode received from the server"); window.location.href = request_uri; // redirect to request uri return false; } return false; }, getFingerprint: function getFingerprint() { // var fp = new Fingerprint({ // canvas: true, // ie_activex: true, // screen_resolution: true // }); var uid = ''; return uid; }, IeLoginBtn: function IeLoginBtn() { var login_link = document.getElementById("login_lnik"); if (login_link) { var check_login_cookie = ssoUI.CookieRead('access_token'); if (check_login_cookie !== '') { login_link.style.display = "none"; } else { login_link.style.display = "block"; } } }, subscriptionListingCheck: function subscriptionListingCheck() { var oauth_user_info = this.CookieRead('oauth_user'); if ('' !== oauth_user_info) { var parsed_info = JSON.parse(oauth_user_info); var not_valid = ['created', 'authenticated', 'pending', 'halted', 'active']; if (typeof parsed_info.plan_status != 'undefined' && not_valid.indexOf(parsed_info.plan_status) > -1) { this.subscriptionGaEvent('Subscription Activity', 'Epaper viewed', '', ''); location.href = '/express-plus/today-newsstand/'; } else { this.subscriptionGaEvent('Subscription Activity', 'Plans viewed', '', ''); if ('1' !== this.CookieRead('subscription_popup')) { location.href = '/express-plus/subscription-plan/#goofy'; } else { location.href = '/express-plus/subscription-plan/'; } } } else { this.subscriptionGaEvent('Subscription Activity', 'Plans viewed', '', ''); if ('1' !== this.CookieRead('subscription_popup')) { location.href = '/express-plus/subscription-plan/#goofy'; } else { location.href = '/express-plus/subscription-plan/'; } } return false; }, subscriptionGaEvent: function subscriptionGaEvent(category, action, label, value) { if ('undefined' !== typeof ga) { ga('send', { 'hitType': 'event', 'eventCategory': category, 'eventAction': action, 'eventLabel': label, 'useBeacon': true, 'hitCallback': function() { googleSent = true; } }); } return true; }, trueSDKEvent: function trueSDKEvent( redirectURL ) { this.access_token = ssoUI.CookieRead( 'access_token' ); if ( this.access_token !== '' && 'undefined' !== typeof this.access_token && null !== this.access_token && 'undefined' !== typeof redirectURL ) { location.href = redirectURL; } else if ('undefined' !== typeof redirectURL ) { location.href = this.site_url + "/?truesdk=1&request_uri=" + redirectURL; } }, getParentDomainName: function getParentDomainName() { var alldomain = window.location.hostname; if( alldomain.indexOf('financialexpress') > -1 ) { return 'financialexpress.com'; } else if ( alldomain.indexOf('indianexpress') > -1 ) { return 'indianexpress.com'; } else { return alldomain; } } }; ssoUI.init();